package cn.innosmart.aq.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

/**
 * Created by David on 2015/9/22.
 */
public class AppUtil {
    private AppUtil() {

    }

    public static AppUtil getInstance() {
        return AppUtilInstance.instance;
    }

    public static boolean isEmulator() {
        return Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.startsWith("unknown")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk".equals(Build.PRODUCT);
    }

    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    public String getVersion(Context context, int type) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            if (type == 0) {
                int index = version.lastIndexOf(".");
                version = version.substring(0, index);
            } else if (type == 1) {

            }
            return "V" + version;
        } catch (Exception e) {
            e.printStackTrace();
            return "UnKnown";
        }
    }


    private static class AppUtilInstance {
        private static AppUtil instance = new AppUtil();
    }
}
